﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;

[RequireComponent(typeof(GUIText))]
[ExecuteInEditMode]
public class ToolTips : MonoBehaviour
{

    // Use this for initialization
      
    public int ElementToEdit = 0;
    private int lastElement = 0;
    public int TotalElements;
    private float delayTimer;
    private float AliveTimer = 0;
    public bool AddTip = false;
    public string typeDeleteToRemove;
    
    public List<TipText> tipText = new List<TipText>();

    private float makeXthis, makeYthis, makeX2this, makeY2this;
    private bool startTimer = false;
    public bool ButtonDestroy = false;
    

    [System.Serializable]
    public class TipText
    {
        public float delayTime = 0f;
        public float AliveTime = 3f;
        [HideInInspector]
        public float waitBetweenMsgs = 1f;
       [HideInInspector]
        public Texture guiTexture;
        [HideInInspector]
        public Rect textureOffset;
        [HideInInspector]
        public string text = "Put text here";
        [HideInInspector]
        public TextAnchor anchor = TextAnchor.UpperLeft;
        [HideInInspector]
        public TextAlignment alignment = TextAlignment.Center;
        [HideInInspector]
        public Vector2 pixelOffset;
        [HideInInspector]
        public float lineSpacing = 1f;
        [HideInInspector]
        public float tabSize = 4f;
        [HideInInspector]
        public Font font;
        [HideInInspector]
        public Material material;
        [HideInInspector]
        public int fontSize;
        [HideInInspector]
        public FontStyle fontStyle = FontStyle.Normal;
        [HideInInspector]
        public Color color = Color.white;
        [HideInInspector]
        public float textAlpha = 1f;
        [HideInInspector]
        public bool richText = true;


    }
    void Awake()
    {
    }
    void Start()
    {

        if (Application.isPlaying)
        {
            //print(Screen.height+" f");
            Rect r = guiText.GetScreenRect();
            //Debug.Log("This gui element is " + r.width + " pixel wide.");

            SynchGUIText();
            guiText.enabled = false;
            if (guiTexture != null)
            {
                guiTexture.enabled = false;
            }
            if (tipText.Count == 0)
            {
                //Debug.LogError("Tip text list size is 0 please change on the " + gameObject.name + " gameobject");
            }
        }
    }



    IEnumerator UpdateList()
    {

        yield return new WaitForSeconds(tipText[0].waitBetweenMsgs);
        tipText.RemoveAt(0);
        SynchGUIText();
    }
    void AddToolTip()
    {
        tipText.Add(tipText[ElementToEdit]);
        //tipText.Add(new TipText());
    }
    void RemoveTip()
    {
        tipText.RemoveAt(ElementToEdit);
    }
    void Synch()
    {
        if (guiTexture != null)
        {
                  tipText[ElementToEdit].guiTexture = guiTexture.texture;
            tipText[ElementToEdit].textureOffset = new Rect(Screen.width / guiTexture.pixelInset.x, Screen.height / guiTexture.pixelInset.y, Screen.width / guiTexture.pixelInset.width, Screen.height / guiTexture.pixelInset.height);
        }
        tipText[ElementToEdit].text = guiText.text;
        wordWrap();
        //tipText[ElementToEdit].text = tipText[ElementToEdit].text.Replace("___", "\n");
        tipText[ElementToEdit].anchor = guiText.anchor;
        tipText[ElementToEdit].alignment = guiText.alignment;
        tipText[ElementToEdit].pixelOffset = new Vector2(Screen.width / guiText.pixelOffset.x, Screen.height / guiText.pixelOffset.y);
        tipText[ElementToEdit].lineSpacing = guiText.lineSpacing;
        tipText[ElementToEdit].tabSize = guiText.tabSize;
        tipText[ElementToEdit].font = guiText.font;
        //tipText[ElementToEdit].material = guiText.material;
        tipText[ElementToEdit].fontSize = guiText.fontSize;
        tipText[ElementToEdit].fontStyle = guiText.fontStyle;
        tipText[ElementToEdit].color = guiText.color;
        tipText[ElementToEdit].richText = guiText.richText;
        tipText[ElementToEdit].color = new Color(tipText[ElementToEdit].color.r, tipText[ElementToEdit].color.g, tipText[ElementToEdit].color.b, guiText.color.a);
    }

    void SynchGUIText()
    {
        if (guiTexture != null)
        {
            guiTexture.enabled = true;
            guiTexture.texture = tipText[0].guiTexture;
            guiTexture.pixelInset = new Rect(Screen.width / tipText[0].textureOffset.x, Screen.height / tipText[0].textureOffset.y, Screen.width / tipText[0].textureOffset.width, Screen.height / tipText[0].textureOffset.height);
        }
        guiText.text = tipText[0].text;
        wordWrap();
        //guiText.text = guiText.text.Replace("___", "\n");
        guiText.anchor = tipText[0].anchor;
        guiText.alignment = tipText[0].alignment;
        guiText.pixelOffset = new Vector2(Screen.width / tipText[0].pixelOffset.x, Screen.height / tipText[0].pixelOffset.y);
        guiText.lineSpacing = tipText[0].lineSpacing;
        guiText.tabSize = tipText[0].tabSize;
        guiText.font = tipText[0].font;
        guiText.material = tipText[0].material;
        guiText.fontSize = tipText[0].fontSize;
        guiText.fontStyle = tipText[0].fontStyle;
        guiText.color = tipText[0].color;
        guiText.richText = tipText[0].richText;
        guiText.color = new Color(guiText.color.r, guiText.color.g, guiText.color.b, tipText[0].textAlpha);
    }
    void EditSynch()
    {
        if (guiTexture != null)
        {
            guiTexture.texture = tipText[ElementToEdit].guiTexture;
            guiTexture.pixelInset = new Rect(Screen.width / tipText[ElementToEdit].textureOffset.x, Screen.height / tipText[ElementToEdit].textureOffset.y, Screen.width / tipText[ElementToEdit].textureOffset.width, Screen.height / tipText[ElementToEdit].textureOffset.height);
        }
        guiText.text = tipText[ElementToEdit].text;
        wordWrap();
        //guiText.text = guiText.text.Replace("___", "\n");
        guiText.anchor = tipText[ElementToEdit].anchor;
        guiText.alignment = tipText[ElementToEdit].alignment;
        guiText.pixelOffset = new Vector2(Screen.width / tipText[ElementToEdit].pixelOffset.x, Screen.height / tipText[ElementToEdit].pixelOffset.y);
        guiText.lineSpacing = tipText[ElementToEdit].lineSpacing;
        guiText.tabSize = tipText[ElementToEdit].tabSize;
        guiText.font = tipText[ElementToEdit].font;
        //guiText.material = tipText[ElementToEdit].material;
        guiText.fontSize = tipText[ElementToEdit].fontSize;
        guiText.fontStyle = tipText[ElementToEdit].fontStyle;
        guiText.color = tipText[ElementToEdit].color;
        guiText.richText = tipText[ElementToEdit].richText;
        guiText.color = new Color(guiText.color.r, guiText.color.g, guiText.color.b, tipText[ElementToEdit].textAlpha);
    }
    // Update is called once per frame
    void Update()
    {
        if (!Application.isPlaying)
        {
            if ((ElementToEdit + 1) > tipText.Count || ElementToEdit < 0)
            {
                ElementToEdit = 0;
            }
            if (ElementToEdit != lastElement)
            {
                EditSynch();
                lastElement = ElementToEdit;
            }
            else if(tipText.Count > 0)
            {
                Synch();
            }
            if (AddTip)
            {
                AddTip = false;
                AddToolTip();
            }
            if (typeDeleteToRemove.ToLower() == "delete")
            {
                typeDeleteToRemove = "";
                RemoveTip();
            }
            TotalElements = tipText.Count;

            if (guiText != null)
            {
                makeXthis = Screen.width / guiText.pixelOffset.x;
                makeYthis = Screen.height / guiText.pixelOffset.y;
                print(Screen.height);
                tipText[ElementToEdit].pixelOffset = new Vector2(makeXthis, makeYthis);
                EditSynch();
            }
            if(guiTexture != null)
            {
                makeX2this = Screen.width / guiTexture.pixelInset.x;
                makeY2this = Screen.height / guiTexture.pixelInset.y;
                EditSynch();
            }

                
            // tipText[0].pixelOffset = guiText.pixelOffset;
        }
        if (Application.isPlaying)
        {
            if (guiTexture != null)
            {
                makeXthis = Screen.width / guiTexture.pixelInset.x;
                makeYthis = Screen.height / guiTexture.pixelInset.y;
            }
            if (guiText != null)
            {
                makeXthis = Screen.width / guiText.pixelOffset.x;
                makeYthis = Screen.height / guiText.pixelOffset.y;
            }
            //SynchGUIText();
            //guiText.text = guiText.text.Replace("___", "\n");
            delayTimer += Time.deltaTime;

            if (delayTimer >= tipText[0].delayTime && !guiText.enabled)
            {
                guiText.enabled = true;
                if (guiTexture != null)
                    guiTexture.enabled = true;
                startTimer = true;
            }
            if (startTimer)
            {
                AliveTimer += Time.deltaTime;
            }

            if (AliveTimer >= tipText[0].AliveTime && guiText.enabled)
            {
                if (tipText.Count > 1)
                {
                    guiText.text = "";
                    if (guiTexture != null)
                    {
                        guiTexture.enabled = false;
                    }
                    StartCoroutine(UpdateList());
                }
                else if(!ButtonDestroy)
                {
                    Destroy(gameObject);
                }
                AliveTimer = 0;
                delayTimer = 0;
            }
        }
    }
    public void wordWrap()
    {
        string copy = guiText.text;
        string[] words = guiText.text.Split(' ');

        string result = "";

        Rect textArea = new Rect();



        for (int i = 0; i < words.Length; i++)
        {

            // set the gui text to the current string including new word

            guiText.text = (result + words[i] + " ");

            // measure it

          textArea = guiText.GetScreenRect();
            //print("cam:" + Camera.main.pixelWidth + " " + Screen.width);
            // if it didn't fit, put word onto next line, otherwise keep it
            if (textArea.width + textArea.x >= Screen.width)
            {
                //print("I GET HERE");
                result += ("\n" + words[i] + " ");

            }

            else
            {

                result = guiText.text;

            }
        }
        guiText.text = result;
    }
}
